﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Demo01.Repsotories;
using WindowsFormsApp1.Models;
using WindowsFormsApp1.Respostories;

namespace WindowsFormsApp1.Respostories
{
    public static class DataRepostory
    {
        public static List<Data> GetData()
        {
            List<Data> Data = new List<Data>();
            SqlDataReader reader = null;
            try
            {
                reader = DBHelper.Query("SELECT Id, SlaveId,Temperature, Humidity,Creation FROM THRecords ORDER BY SlaveId ASC");
                while (reader.Read())
                {
                    Data data = new Data
                    {
                        Id = reader.GetInt32(0).ToString(),
                        SlaveId = reader.GetInt32(1).ToString() + "号仓库",
                        Temperature = reader.GetDouble(2).ToString(),
                        Humidity = reader.GetDouble(3).ToString(),
                        Creation = reader.GetDateTime(4)
                    };
                    Data.Add(data);
                }
            }
            finally
            {
                reader?.Close();
            }

            return Data;

        }

       /// <summary>
       /// 根据分类ID、起始时间、结束时间获取数据
       /// </summary>
       /// <param name="categorySlaveId">分类ID</param>
       /// <param name="dtpStarttime"> 起始时间</param>
       /// <param name="dtpEndtime">结束时间</param>
       /// <returns></returns>
        public static List<Data> GetData(string categorySlaveId = "", DateTime? dtpStarttime = null, DateTime? dtpEndtime = null)
        {
            List<Data> Data = new List<Data>();
            SqlDataReader reader = null;
            int SlaveId = 0;
            if (categorySlaveId == "1号仓库")
            {
                SlaveId = 1;
            }
            else if (categorySlaveId == "2号仓库")
            {
                SlaveId = 2;
            }
            else if (categorySlaveId == "3号仓库")
            {
             
                SlaveId = 3;
            }
             else
            {
                SlaveId = 0;
            }


            try
            {
                string sql = "SELECT Id, SlaveId,Temperature, Humidity,Creation FROM THRecords WHERE";

                if (dtpStarttime.HasValue)
                {
                    sql += "  Creation >= @dtpStarttime";
                }
                if (dtpEndtime.HasValue)
                {
                    sql += " AND Creation <= @dtpEndtime";
                }
                if (SlaveId != 0)
                {
                    sql += " AND SlaveId = @SlaveId";
                }
                sql += " ORDER BY SlaveId ASC";
                SqlParameter[] parameters = new SqlParameter[] { };

                if (!string.IsNullOrEmpty(categorySlaveId))
                {
                    parameters = new SqlParameter[] { new SqlParameter("@SlaveId", SlaveId) };
                }
                if (dtpStarttime.HasValue)
                {
                    parameters = parameters.Concat(new SqlParameter[] { new SqlParameter("@dtpStarttime", dtpStarttime.Value) }).ToArray();
                }
                if (dtpEndtime.HasValue)
                {
                    parameters = parameters.Concat(new SqlParameter[] { new SqlParameter("@dtpEndtime", dtpEndtime.Value) }).ToArray();
                }

                reader = DBHelper.Query(sql, parameters);
                while (reader.Read())
                {
                    Data data = new Data
                    {
                        Id = reader.GetInt32(0).ToString(),
                        SlaveId = reader.GetInt32(1).ToString() + "号仓库",
                        Temperature = reader.GetDouble(2).ToString(),
                        Humidity = reader.GetDouble(3).ToString(),
                        Creation = reader.GetDateTime(4)
                    };
                    Data.Add(data);
                }
            }
            finally
            {
                reader?.Close();
            }

            return Data;
        }

        
    }
}
